// 输入框组件实例

import { assignInWith } from 'lodash'
import type { dateRangeConfigType, inputConfigType, radioGroupConfigType, uploadConfigType } from '../../../../../types/index'
import BaseEntity from '../../BaseEntity'
import { uid } from 'uid'
import type { configType } from '../../BaseEntity'
import { entityType } from '../..'
import type {FileItem} from '@arco-design/web-vue'

interface UploadSlotFormConfigType {
  modelValue:FileItem[];
  accept:string;
  action:string;
  multiple:boolean;
  limit:number;
  data:Object;
  headers:Object;
  [key: string]: any
}

export default class UploadEntity extends BaseEntity {
    

    constructor(config: configType, slotFormConfig: UploadSlotFormConfigType) {
        let baseConfig: configType = {
            class: ['draggable-form-hover'],
            style: { width: '100%' },
            field: 'Upload_File_' + uid(4),
            label: '上传文件',
            hideLabel: false,
            required: false,
            rules:[{
              validator: (value, cb) => {
                if (this.config.required&&value.length==0) {
                  cb('请上传文件')
                } else {
                  cb()
                }
              }
            }]
        }
        let baseSlotFormConfig: UploadSlotFormConfigType = {
            modelValue: [],
            accept:"",
            action:"",
            headers:{
              token:"xxx"
            },
            data: {},
            multiple:true,
            limit:0,
          
     
        }
        let componentConfig: uploadConfigType = {
          accept:{
            type:'input',
            formItemConfig: {
                label: '限制上传文件类型'
            },
            formItemElement: {
                modelValue: "",
                placeholder: '请输入'
            }
          },
          action:{
            type:'input',
            formItemConfig: {
                label: '上传地址'
            },
            formItemElement: {
                modelValue: "",
                placeholder: '请输入上传地址'
            }
          },
          multiple:{
            type:'switch',
            formItemConfig: {
                label: '是否支持多个文件上传'
            },
            formItemElement: {
                modelValue: false,
                
            }
          },
          limit:{
            type:'inputNumber',
            formItemConfig: {
                label: '限制上传数量(0表示不限制上传数量)'
            },
            formItemElement: {
                modelValue: 0,
                placeholder:'请输入上传数量',
            }
          },
          headers:{
            type:'optionsObjectSetting',
            formItemConfig: {
                label: '上传请求头'
            },
            formItemElement: {
                modelValue: {},
                placeholder: '请输入'
            }
          },
            data:{
               type:'optionsObjectSetting',
                formItemConfig: {
                    label: '额外上传数据'
                },
                formItemElement: {
                    modelValue: {},
                    placeholder: '请输入'
                }
            }
           
        }

        super(
          entityType.upload,
            assignInWith(baseConfig, config),
            assignInWith(baseSlotFormConfig, slotFormConfig),
            componentConfig
        )
        this.initSlotFormConfig()
    }
    initSlotFormConfig(){
      //初始化表单组件可配置项 因为表单组件可配置项是根据表单组件所拥有得属性来定义的 配置项得值需要对应初始化实例得对应属性得值
      Object.keys(this.componentConfig).forEach(key=>{
        if( this.slotFormConfig[key]){
          this.componentConfig[key].formItemElement.modelValue= this.slotFormConfig[key]
        }
      })


    }
}
